<?php

namespace Core\init;

use Core\annotations\Bean;
use Core\BeanFactory;

/**
 * Class MyRedis
 * @Bean
 */
class MyRedis
{
    /**
     * @var PhpRedisPool
     */
    private $pool;

    public function __construct()
    {
        $redis = BeanFactory::getBean(PhpRedisPool::class);
        $this->pool = $redis;
    }


    /**
     * 调用Redis 相关方法
     * @param $methodName
     * @param $arguments
     * @return array
     */
    public function __call($methodName, $arguments)
    {
        $redis_obj = $this->pool->getConnect();
        try {
            if (!$redis_obj) {
                return [];
            }
            $redis = $redis_obj->redis;
            return $redis->$methodName(...$arguments);
        } catch (\Exception $exception) {
            echo "异常信息:" . $exception->getMessage() . ",异常文件:" . $exception->getFile() . ",异常行数:" . $exception->getLine();
        } finally {
            if ($redis_obj) {
                $this->pool->close($redis_obj);
            }
        }
    }
}
